from django.http import JsonResponse
from django.shortcuts import render, redirect
from django.urls import reverse
from django.views.decorators.csrf import csrf_exempt

from web.forms.wiki import WikiModelForm
from web import models
from utils.cos import upload_file
from utils.encrypt import uid


def wiki(request, project_id):
    wiki_id = request.GET.get('wiki_id')
    if not wiki_id or models.Wiki.objects.filter(id=wiki_id).first().project_id != project_id:
        return render(request, 'wiki.html')
    wiki_object = models.Wiki.objects.filter(id=wiki_id).first()
    return render(request, 'wiki.html', {"wiki_object": wiki_object})


def wiki_add(request, project_id):
    if request.method == 'GET':
        form = WikiModelForm(request)
        return render(request, 'wiki_form.html', {'form': form})
    form = WikiModelForm(request, data=request.POST)
    if form.is_valid():
        form.instance.project = request.tracer.project
        if form.instance.parent:
            form.instance.depth = form.instance.parent.depth + 1
        form.save()
        url = reverse('wiki', kwargs={'project_id': project_id})
        return redirect(url)
    return render(request, 'wiki_form.html', {'form': form})


def wiki_catalog(request, project_id):
    data = (models.Wiki.objects.filter(project_id=project_id).all().values('id', 'title', 'parent_id')
            .order_by('depth', 'id'))
    return JsonResponse({'status': True, 'data': list(data)})


def wiki_delete(request, project_id, wiki_id):
    models.Wiki.objects.filter(project_id=project_id, id=wiki_id).delete()
    url = reverse('wiki', kwargs={'project_id': project_id})
    return redirect(url)


def wiki_edit(request, project_id, wiki_id):
    wiki_object = models.Wiki.objects.filter(project_id=project_id, id=wiki_id).first()
    if not wiki_object:
        return redirect(reverse('wiki', kwargs={'project_id': project_id}))
    if request.method == 'GET':
        form = WikiModelForm(request, instance=wiki_object)
        return render(request, 'wiki_form.html', {'form': form})
    form = WikiModelForm(request, data=request.POST, instance=wiki_object)
    if form.is_valid():
        if form.instance.parent:
            form.instance.depth = form.instance.parent.depth + 1
        form.save()
        return redirect(reverse('wiki', kwargs={'project_id': project_id}) + f'?wiki_id={wiki_id}')
    return render(request, 'wiki_form.html', {'form': form})


@csrf_exempt
def wiki_upload(request, project_id):
    img_object = request.FILES.get('editormd-image-file')
    result = {
        'success': 0,
        'massage': None,
        'url': None,
    }
    if not img_object:
        result['massage'] = '图片不存在'
        return JsonResponse(result)
    ext = img_object.name.rsplit('.')[-1]
    key = "{}.{}".format(uid(request.tracer.user.mobile_phone), ext)
    result['url'] = upload_file(request.tracer.project.bucket, request.tracer.project.region,
                                img_object, key)
    request['success'] = 1
    return JsonResponse(result)
